package com.controller;
import com.entity.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @RequestMapping("/test")
    public String test() {
        return "redirect:/listUsers";
    }

    @GetMapping("/listUsers")
    public String listUsers(HttpServletRequest requeset) {
        List<User> userList = userService.listUsers();
        requeset.setAttribute("userList",userList);
        return "/userList";
    }

    @PostMapping("/addUser")
    public String addUser(User user) {
        userService.addUser(user);
//        return "register_result";
        return "redirect:/listUsers";
    }

    @GetMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable Integer id) {
        userService.deleteUser(id);
        return "redirect:/listUsers";
    }
}
